package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.service.SetmealService;
import constant.MessageConstant;
import constant.RedisConstant;
import entity.PageResult;
import entity.QueryPageBean;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import pojo.Setmeal;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import utils.QiniuUtils;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    
    //@Reference是ubbo注解，注入分布式中的远程服务对象
    // 告诉注册中心会去调用SetmealService的服务接口，封装为对象setmealService
    @Reference
    private SetmealService setmealService;
    
    @Autowired
    private JedisPool jedisPool;
    
    /**
     * 图片上传到七牛云
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile")MultipartFile imgFile){
        //获取原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        System.out.println("原始文件名：" + originalFilename);
        int lastIndexOf = originalFilename.lastIndexOf(".");
        
        //获取文件后缀
        String suffx = originalFilename.substring(lastIndexOf);
        
        //使用UUID随机产生文件名称，防止同文件名覆盖
        String fileName = UUID.randomUUID().toString() + suffx;
        System.out.println("fileName:=========" + fileName);
      
        //七牛云图片上传
        
        try {
            if(QiniuUtils.upload2Qiniu(imgFile.getBytes() ,fileName)){
                System.out.println("true,图片上传成功到七牛云");
                //图片上传成功
                
                if(fileName != null){
                    //存储到Redis,用于GC
                    Jedis jedis = jedisPool.getResource();
                    try{
                        jedis.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
    
                        System.out.println("jedis.smembers(RedisConstant.SETMEAL_PIC_RESOURCES):");
                        System.out.println(jedis.smembers(RedisConstant.SETMEAL_PIC_RESOURCES));
                    }catch (Exception e){
                        System.out.println("redis保存七牛云图片出现异常");
                        e.printStackTrace();
                    }
                }
                return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
            }
        } catch (IOException e) {
            System.out.println("有异常");
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        
    }
    
    /**
     * 添加套餐，图片上传到七牛云后也保存在Mysql数据库
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgropIds){
        
        System.out.println("checkgropIds:"  +checkgropIds.length);
        try{
            setmealService.add(setmeal,checkgropIds);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        }catch (Exception e){
            System.out.println("有异常： ");
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
    }
    
    /**
     * 分页条件查询
     */
    @RequestMapping("/pageQuery")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = null;
        try{
            pageResult = setmealService.pageQuery(queryPageBean);
        }catch (Exception e){
            e.printStackTrace();
        }
        return pageResult;
    }
    
    /**
     * 查询当前套餐包含的检查组
     */
    @RequestMapping("/findCheckgroupsWithSetmealId")
    public Result findCheckgroupsWithSetmealId(Integer setmealId){
        try {
            Integer[] checkgroupIds = setmealService.findCheckgroupsWithSetmealId(setmealId);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }
    
    /**
     * 删除
     */
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try{
            setmealService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
    }
    
    /**
     * 编辑,更新
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try {
            setmealService.edit(setmeal,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }
    
}
